<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>好物到家</title>
    <%-- 导入css --%>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/client/css/main.css" type="text/css"/>

    <link href="../jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <script type="text/javascript" src="../jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="../jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <script>
        $(function () {
            uname = sessionStorage.getItem("username")
            passwo = sessionStorage.getItem("password")
            $("#selfname").html(uname)
            $("#oldpwd").val(passwo)

            var cartdata
            $.ajax({
                url: "${pageContext.request.contextPath}/settings/cart/findallcart",
                data: {
                    "data": "data"
                },
                type: "get",
                dataType: "json",
                success: function (data) {
                    console.log(data)
                    cartdata = data
                    var total = 0
                    if (data != null) {
                        $("#gname0").html(data[0].bname)
                        $("#sp0").html(data[0].bnumber)
                        $("#gprice0").html(data[0].bprice)
                        var count1 = parseInt(data[0].bnumber) * parseFloat(data[0].bprice)
                        $("#gcount0").html(count1)
                        total = parseFloat(count1) + parseFloat(total)
                        if (data[1] != null) {
                            $("#gname1").html(data[1].bname)
                            $("#sp1").html(data[1].bnumber)
                            $("#gprice1").html(data[1].bprice)
                            var count1 = parseInt(data[1].bnumber) * parseFloat(data[1].bprice)
                            $("#gcount1").html(parseInt(data[1].bnumber) * parseFloat(data[1].bprice))
                            total = parseFloat(count1) + parseFloat(total)
                        }
                        if (data[2] != null) {
                            $("#gname2").html(data[2].bname)
                            $("#sp2").html(data[2].bnumber)
                            $("#gprice2").html(data[2].bprice)
                            var count1 = parseInt(data[2].bnumber) * parseFloat(data[2].bprice)
                            $("#gcount2").html(parseInt(data[2].bnumber) * parseFloat(data[2].bprice))
                            total = parseFloat(count1) + parseFloat(total)
                        }
                        if (data[3] != null) {
                            $("#gname3").html(data[3].bname)
                            $("#sp3").html(data[3].bnumber)
                            $("#gprice3").html(data[3].bprice)
                            var count1 = parseInt(data[3].bnumber) * parseFloat(data[3].bprice)
                            $("#gcount3").html(parseInt(data[3].bnumber) * parseFloat(data[3].bprice))
                            total = parseFloat(count1) + parseFloat(total)
                        }
                        if (data[4] != null) {
                            $("#gname4").html(data[4].bname)
                            $("#sp4").html(data[4].bnumber)
                            $("#gprice4").html(data[4].bprice)
                            var count1 = parseInt(data[4].bnumber) * parseFloat(data[4].bprice)
                            $("#gcount4").html(parseInt(data[4].bnumber) * parseFloat(data[4].bprice))
                            total = parseFloat(count1) + parseFloat(total)
                        }

                    }

                    $("#totalmoney").html(total)
                    $("#msp1").html(total)
                }

            })

            /*对商品数量进行增加减少*/
            $("#bu0").click(function () {
                var bu0d = $("#sp0").html()
                var na = $("#gname0").html()
                updatenum(parseInt(bu0d) - 1, na, "#sp0")
            })
            $("#bu00").click(function () {
                var bu0d = $("#sp0").html()
                var na = $("#gname0").html()
                updatenum(parseInt(bu0d) + 1, na, "#sp0")
            })

            $("#bu1").click(function () {
                var bu0d = $("#sp1").html()
                var na = $("#gname1").html()
                updatenum(parseInt(bu0d) - 1, na, "#sp1")
            })
            $("#bu11").click(function () {
                var bu0d = $("#sp1").html()
                var na = $("#gname1").html()
                updatenum(parseInt(bu0d) + 1, na, "#sp1")
            })

            $("#bu2").click(function () {
                var bu0d = $("#sp2").html()
                var na = $("#gname2").html()
                updatenum(parseInt(bu0d) - 1, na, "#sp2")
            })
            $("#bu22").click(function () {
                var bu0d = $("#sp2").html()
                var na = $("#gname2").html()
                updatenum(parseInt(bu0d) + 1, na, "#sp2")
            })

            $("#bu3").click(function () {
                var bu0d = $("#sp3").html()
                var na = $("#gname3").html()
                updatenum(parseInt(bu0d) - 1, na, "#sp3")
            })
            $("#bu33").click(function () {
                var bu0d = $("#sp3").html()
                var na = $("#gname3").html()
                updatenum(parseInt(bu0d) + 1, na, "#sp3")
            })

            $("#bu4").click(function () {
                var bu0d = $("#sp4").html()
                var na = $("#gname4").html()
                updatenum(parseInt(bu0d) - 1, na, "#sp4")
            })
            $("#bu44").click(function () {
                var bu0d = $("#sp4").html()
                var na = $("#gname4").html()
                updatenum(parseInt(bu0d) + 1, na, "#sp4")
            })


            //清空购物车
            $("#clearcart").click(function () {
                deleteAllCartGoods();
            })
            //开始结账
            $("#payit").click(function () {
                orderpay();
            })

            //修改密码
            $("#uppwd").click(function () {
                updatepwd()
            })
        })

        function updatenum(num, nam, sp) {
            console.log("名字=" + nam)
            console.log("数量=" + num)
            console.log("span=" + sp)
            if (parseInt(num) == 0) {
                //如果商品数量为0 则在购物车删除该商品
                deltegoods(nam);
                return false;
            }
            var bu0d = $(sp).html(num)
            $.ajax({
                url: "${pageContext.request.contextPath}/settings/cart/updateCart",
                data: {
                    "num": num,
                    "name": nam
                },
                type: "get",
                dataType: "json",
                success: function (data) {
                    datasource = data;
                    console.log(data)
                    window.location.reload()
                }
            })

        }

        function deltegoods(nam) {
            $.ajax({
                url: "${pageContext.request.contextPath}/settings/cart/deleteCartGoods",
                data: {
                    "name": nam
                },
                type: "get",
                dataType: "json",
                success: function (data) {
                    window.location.reload()
                }
            })
        }

        function deleteAllCartGoods() {
            $.ajax({
                url: "${pageContext.request.contextPath}/settings/cart/deleteAllCartGoods",
                data: {},
                type: "get",
                dataType: "json",
                success: function (data) {
                    datasource = data;
                    console.log(data)
                    alert(data)
                    window.location.reload()
                }
            })
        }

        function orderpay() {
            var address = $.trim($("#address").val());
            var phone = $.trim($("#phone").val());
            var username = $.trim($("#username").val());
            var totalprice = $("#totalmoney").html();
            /*  alert('地址='+address+" "+"手机号码=" +phone+"  "+"收获姓名"+username+"   "+"总价="+totalprice)*/
            var uid = sessionStorage.getItem("uid");

            $.ajax({
                url: "${pageContext.request.contextPath}/settings/cart/insertorders",
                data: {
                    "uid": uid,
                    "address": address,
                    "phone": phone,
                    "username": username,
                    "omoney": totalprice
                },
                type: "get",
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        alert("结账成功")
                        //如果结账成功 则清空购物车
                        deleteAllCartGoods()
                    }
                }
            })

        }

        function updatepwd() {
            var newp = $.trim($("#newpwd").val());
            var nam11 = sessionStorage.getItem("username");
            $.ajax({
                url: "${pageContext.request.contextPath}/settings/user/updatePassword",
                data: {
                    "username": nam11,
                    "newpwd": newp
                },
                type: "get",
                dataType: "json",
                success: function (data) {
                    console.log(data)
                    if (data.success){
                        sessionStorage.setItem("password",newp)
                        alert("更新密码成功")
                    }

                }
            })
        }
    </script>

</head>

<body class="main">
<%@include file="head.jsp" %>
<%@include file="menu_search.jsp" %>

<div>
    <%--修改信息模态框--%>
    <div class="modal fade" id="selfmessage" role="dialog">
        <div class="modal-dialog" role="document" style="width: 30%;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">×</span>
                    </button>
                    <h4 class="modal-title">个人信息</h4>
                </div>
                <div class="modal-body">
                    <div style="text-align: left">
                        <br>
                        <label>用户名:
                            <sapn id="selfname">123</sapn>
                        </label>
                        <br>

                        <br>
                        <label>原密码:
                            <sapn id="oldpwd">123</sapn>
                        </label>
                        <br>
                        <br>
                        <label>新密码:</label>
                        <input type="text" id="newpwd" placeholder="请输入新密码">
                        <br>
                    </div>
                </div>
                <div class="modal-footer">
                    <button id="uppwd" type="button" class="btn btn-primary" data-dismiss="modal">确定修改</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <%--开始结账模态框--%>
    <div class="modal fade" id="payment" role="dialog">
        <div class="modal-dialog" role="document" style="width: 30%;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">×</span>
                    </button>
                    <h4 class="modal-title">订单支付</h4>
                </div>
                <div class="modal-body">
                    <div style="text-align: left">
                        <span style="font-size: 20px;"><font color="#deb887">总计:</font></span><span id="msp1"
                                                                                                    style="font-size: 20px">1</span>元<br>
                        <br>
                        <label>收获地址:</label>
                        <input type="text" id="address" placeholder="请输入收获地址">
                        <br>
                        <br>
                        <label>联系电话:</label>
                        <input type="text" id="phone" placeholder="请输入联系电话">
                        <br>
                        <br>
                        <label>收获姓名:</label>
                        <input type="text" id="username" placeholder="请输入姓名">
                        <br>
                        <br>
                    </div>
                </div>
                <div class="modal-footer">
                    <button id="payit" type="button" class="btn btn-primary" data-dismiss="modal">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>


    <table class="table table-hover" id="boottable">
        <thead>
        <tr style="color: #B3B3B3;">
            <td>商品名字</td>
            <td>商品数量</td>
            <td>商品价格</td>
            <td>商品小计</td>
        </tr>
        </thead>
        <tbody>
        <tr>

            <td id="gname0"><a style="text-decoration: none; cursor: pointer;"
                               onclick="window.location.href='detail.html';"></a></td>
            <td id="gnum0">
                <button id="bu0" type="button" class="btn btn-sm">一</button>
                <span id="sp0">0</span>
                <button id="bu00" type="button" class="btn  btn-sm">+</button>
            </td>
            <td id="gprice0">0</td>
            <td id="gcount0">0</td>
        </tr>
        <tr>

            <td id="gname1"><a style="text-decoration: none; cursor: pointer;"
                               onclick="window.location.href='detail.html';"></a></td>
            <td id="gnum1">
                <button id="bu1" type="button" class="btn btn-sm">一</button>
                <span id="sp1">0</span>
                <button id="bu11" type="button" class="btn  btn-sm">+</button>
            </td>
            <td id="gprice1">0</td>
            <td id="gcount1">0</td>
        </tr>
        <tr>

            <td id="gname2"><a style="text-decoration: none; cursor: pointer;"
                               onclick="window.location.href='detail.html';"></a></td>
            <td id="gnum2">
                <button id="bu2" type="button" class="btn btn-sm">一</button>
                <span id="sp2">0</span>
                <button id="bu22" type="button" class="btn  btn-sm">+</button>
            </td>
            <td id="gprice2">0</td>
            <td id="gcount2">0</td>
        </tr>
        <tr>

            <td id="gname3"><a style="text-decoration: none; cursor: pointer;"
                               onclick="window.location.href='detail.html';"></a></td>
            <td id="gnum3">
                <button id="bu3" type="button" class="btn btn-sm">一</button>
                <span id="sp3">0</span>
                <button id="bu33" type="button" class="btn  btn-sm">+</button>
            </td>
            <td id="gprice3">0</td>
            <td id="gcount3">0</td>
        </tr>
        <tr>
            <td id="gname4"><a style="text-decoration: none; cursor: pointer;"
                               onclick="window.location.href='detail.html';"></a></td>
            <td id="gnum4">
                <button id="bu4" type="button" class="btn btn-sm">一</button>
                <span id="sp4">0</span>
                <button id="bu44" type="button" class="btn  btn-sm">+</button>
            </td>
            <td id="gprice4">0</td>
            <td id="gcount4">0</td>
        </tr>
        </tbody>
    </table>
    <div style="text-align: right;color: #ff0000;font-size: 20px">订单总价：
        <span id="totalmoney">0</span>
    </div>
    <button id="clearcart" type="button" class="btn btn-danger">清空购物车</button>
    <button id="ordercart" type="button" class="btn btn-primary" data-toggle="modal" data-target="#payment">开始结账
    </button>
</div>

</body>
</html>
